﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class member_invoice : System.Web.UI.Page
{
	public string EnterpriseName = "";
    public string UserID = "";
    public string UserPower = "";
    public string Footer = "";
	
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Init.islogin(ref UserID,ref EnterpriseName,ref UserPower))
        {
            Response.Redirect("/Login.aspx");
        }
        if (!UserPower.Contains("Manager"))
        {
            Response.Write("你没有权限！");
            Response.End();
        }
        //this.EnableViewState = false;
        id = Request.Params["SID"];
        Response.Write(Init.InitDefault());
        totalSize =long.Parse(DataGroupCount());
        
       // int currentPage=
        pagen = Request.QueryString["pagenum"];
        
        if (!string.IsNullOrEmpty(pagen))
        {
            pagenum = long.Parse(pagen);
        }

        DataSet ds = DataGroup(id, pageSize, pagenum-1);
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "djbh" };//主键
        GridView1.DataBind();
        listpager.Text=Init.Pager("invoice.aspx?pagenum={pageNum}", totalSize, pageSize, pagenum);
        

    }
    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();
    private long pageSize = 25;
    long totalSize = 1;
    string pagen = "";
    long pagenum = 0;
    string id = "";
    string djbh = "";
    string dw = "";
    string fp = "";
    string txd = "";
    string crksrq = "";
    string crkerq = "";


    //统计
    public string DataGroupCount(string SID = "")
    {
        djbh = rkd.Text;
        dw = gys.Text;
        fp = f.Text;
        txd = fs.Text;
        crksrq = srq.Text;
        crkerq = erq.Text;
        string sql = "";
        sql += "select count(distinct djbh) "
        + " from spls_rk a(nolock) left join mchk b(nolock) on a.dwbh=b.dwbh where a.djbh like 'JHA%'";
        sql += (!string.IsNullOrEmpty(crksrq)) ? " and a.rq>='" + crksrq + "'" : " and a.rq>=convert(varchar(120),getdate()-3,120) ";
        sql += (!string.IsNullOrEmpty(crkerq)) ? " and a.rq<='" + crkerq + "'" : " and a.rq<=convert(varchar(120),getdate(),120)";
        sql += (!string.IsNullOrEmpty(djbh)) ? " and a.djbh='" + djbh + "'" : "";
        sql += (!string.IsNullOrEmpty(dw)) ? " and (danwbh like '%" + dw + "%' or dwmch like '%" + dw + "%' or zjm like '%" + dw + "%')" : "";
        sql += (fp == "1") ? " and djbh not in(select detailid from [ImageDoc](nolock) where type_id='fa' group by detailid) " : "";
        sql += (fp == "2") ? " and djbh in(select detailid from [ImageDoc](nolock) where type_id='fa' group by detailid) " : "";
        sql += (txd == "1") ? " and djbh not in(select detailid from [ImageDoc](nolock) where type_id='sh' group by detailid) " : "";
        sql += (txd == "2") ? " and djbh in(select detailid from [ImageDoc](nolock) where type_id='sh' group by detailid) " : "";
        sql += (!string.IsNullOrEmpty(SID)) ? " and a.djbh= '" + SID + "' " : "";
        if (Init.IsDebug)
        {
            ResultDeBug.Text = sql;
        }
        return mssql.GetString(sql);
    }


    //展示
    public DataSet DataGroup(string SID = "", long pageSize=10, long pagination=0)
    {
        djbh = rkd.Text;
        dw = gys.Text;
        fp = f.Text;
        txd = fs.Text;
        crksrq = srq.Text;
        crkerq = erq.Text;
        string sql = "";
        sql += "select top " + pageSize + " * from (";
        sql += "select ROW_NUMBER() OVER (ORDER BY a.djbh desc) AS RowNumber,rtrim(a.djbh) as djbh,rtrim(b.dwmch) as dwmch,rtrim(a.rq) as rq,'' as sh,'' as fa,'' as ghssh,''ghsfa,'' as rk "
        + " from spls_rk a(nolock) left join mchk b(nolock) on a.dwbh=b.dwbh where a.djbh like 'JHA%'";
        sql += (!string.IsNullOrEmpty(crksrq)) ? " and a.rq>='" + crksrq + "'" : " and a.rq>=convert(varchar(120),getdate()-3,120) ";
        sql += (!string.IsNullOrEmpty(crkerq)) ? " and a.rq<='" + crkerq + "'" : " and a.rq<=convert(varchar(120),getdate(),120)";
        sql += (!string.IsNullOrEmpty(djbh)) ? " and a.djbh='" + djbh + "'" : "";
        sql += (!string.IsNullOrEmpty(dw)) ? " and (danwbh like '%" + dw + "%' or dwmch like '%" + dw + "%' or zjm like '%" + dw + "%')" : "";
        sql += (fp == "1") ? " and djbh not in(select detailid from [ImageDoc](nolock) where type_id='fa' group by detailid) " : "";
        sql += (fp == "2") ? " and djbh in(select detailid from [ImageDoc](nolock) where type_id='fa' group by detailid) " : "";
        sql += (txd == "1") ? " and djbh not in(select detailid from [ImageDoc](nolock) where type_id='sh' group by detailid) " : "";
        sql += (txd == "2") ? " and djbh in(select detailid from [ImageDoc](nolock) where type_id='sh' group by detailid) " : "";
        sql += (!string.IsNullOrEmpty(SID)) ? " and a.djbh= '" + SID + "' " : "";
        sql += " group by a.djbh,a.rq,b.dwmch ";
        sql += " ) a where RowNumber>" + pageSize + "*" + pagination + " ";
        sql += " order by RowNumber";
        if (Init.IsDebug)
        {
            ResultDeBug.Text = sql;
        }
        DataSet dss = mssql.ExecuteDs(sql);

        
        for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
        {
            dss.Tables[0].Rows[i]["sh"] = herf(dss.Tables[0].Rows[i]["djbh"].ToString(), "sh");
            dss.Tables[0].Rows[i]["fa"] = herf(dss.Tables[0].Rows[i]["djbh"].ToString(), "fa");
            dss.Tables[0].Rows[i]["ghssh"] = herf(dss.Tables[0].Rows[i]["djbh"].ToString(), "ghssh");
            dss.Tables[0].Rows[i]["ghsfa"] = herf(dss.Tables[0].Rows[i]["djbh"].ToString(), "ghsfa");
            dss.Tables[0].Rows[i]["rk"] = herf(dss.Tables[0].Rows[i]["djbh"].ToString(), "rk");
        }
      
        return dss;
    }



    public string herf(string id,string ret)
    {
        DataSet dsmx=new DataSet();
        dsmx.Clear();
        string detailid = id;
        string retsult = "";
        string sql = "select distinct rtrim(imageid) as imageid,rtrim(url) as url from [ImageDoc](nolock) where type_id=rtrim('" + ret + "') and detailid=rtrim('" + detailid + "')";
        dsmx=mssql.ExecuteDs(sql);
        
        
        if (dsmx.Tables[0].Rows.Count == 0)
        {
            retsult += " <a class=\"upload\" href=\"javascript:;\" data-type=\"file\" data-dap=\""+ret+"\" data-id=\"" + id  + "\" style=\"color: #666; border: 1px solid #d2ccc4;display: inline-block;	margin: 5px 0;\">上传</a> ";
        }
        else
        {
         
            retsult += " <a class=\"upload\" href=\"javascript:;\" data-type=\"file\" data-dap=\"" + ret + "\" data-id=\"" + id + "\" style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;	margin: 5px 0;\">上传</a> ";
			int rowid=0;
            for (int i = 0; i < dsmx.Tables[0].Rows.Count; i++)
            {
               string fileurl = Init.imagefileurl(dsmx.Tables[0].Rows[i]["url"].ToString());
                if (!string.IsNullOrEmpty(fileurl))
                {
                   // retsult += " <a class=\"btn-view preview\" href=\"" + fileurl + "\" rel=\"" + dsmx.Tables[0].Rows[i]["rel"].ToString() + "\" " + (rowid == 0 ?"": " style=\"display:none;\"") + ">预览</a>\r\n";
                    retsult += " <a href=\"" + fileurl + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "\"  src=\"" + fileurl + "\" data-dap=\"" + ret + "\"  class=\"view\" " + (rowid == 0 ? "" : " style=\"display:none;\"") + " style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a> ";
                    rowid++;
                }
                /*
                if (i == 0)
                {
                    retsult = retsult + " <a href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "\"  src=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" data-dap=\"" + ret + "\"  class=\"view\"  style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a> ";
                }
                else
                {
                    retsult = retsult + " <a href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "\"  src=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" data-dap=\"" + ret + "\"  class=\"view\" style=\"display:none;\"  style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a> ";
                }
                 */
            }
        }
        return retsult;
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(pagen))
        {
            pagenum = long.Parse(pagen);
        }
        DataSet ds = DataGroup(id, pageSize, pagenum - 1);
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "djbh" };//主键
        int recordCount = GridView1.Rows.Count;
        GridView1.DataBind();
        listpager.Text = Init.Pager("invoice.aspx?pagenum={pageNum}", totalSize, pageSize, pagenum);
    }
}